import {baseImgUrl} from "../apis/api";
// 检测路由
export const watchRouterNews = {
  methods: {
    sss() {
      //console.log("111");
    }
  }
};
// 删除富文本html标签
export const removeTags = {
  filters:{
    removeHtml(val){
      if(val) {
        try {
          return val.replace(/<[^>]+>/g, "");
        }catch(e) {
          return val
        }
      }
    }
  }
};
// 处理文本处理
export const actionCon = {
  filters: {
    /*替换内容中所有的图片的地址*/
    filters2: function (arg) {
      try {
        arg.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/gi, function (match,
                                                                    capture) {
          arg = arg.replace(new RegExp(capture, "g"), baseImgUrl + capture);
        });
        //console.log('aaaaaaa',capture,baseImgUrl)
        return arg.replace(/<[^>]*>|/g, "");
        //console.log(desc)
       // return desc.replace(/(^\s*)|(\s*$)/g, "");
      }catch (e) {
        return ''
      }


      //return arg;
    },
    /*提取内容中所有的描述*/
    con(arg) {
      if (arg) {
        arg.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/gi, function (match,
                                                                    capture) {
                                                                      
        //console.log(baseImgUrl,'~~~~~~~~~~',match,capture)
        if(capture.indexOf('http')=== -1){
          arg = arg.replace(new RegExp(capture, "g"), baseImgUrl + capture);
        }
        });
        return arg;
      }
    }
  }
};
// 常见时间格式化
export const time = {
  filters: {
    /*2018年01月04日*/
    time1(val) {
      if(val) {
        try {
          let str = val.split(" ")[0].split("-");
          return str[0] + "年" + str[1] + "月" + str[2] + "日";
        }catch(e) {
          return val
        }
      }
    },
    /*2018.01.04*/
    time2(val) {
      let str = val.split(" ")[0].split("-");
      return str[0] + "." + str[1] + "." + str[2];
    },
    /*2018-01-04*/
    time3(val) {

      //if(! val instanceof Number)
      if( typeof val != 'number') {
        return val.split(" ")[0];
      }else {
        var date = new Date(val);
        return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
      }

    },
    /*2017年11月27日  10：06*/
    time4(val) {
      let str = val.split(" ");
      let strs = str[0].split("-");
      let strss = str[1].split(":");
      return (
        strs[0] +
        "年" +
        strs[1] +
        "月" +
        strs[2] +
        "日 " +
        " " +
        strss[0] +
        ":" +
        strss[1]
      );
    },
    // 处理毫秒数
    crtTimeFtt(val, row) {
      if (val) {
        var date = new Date(val);
        let M = 0, D = 0
        if((date.getMonth() + 1) < 10) {
          M = '0'+ (date.getMonth() + 1)
        }else {
          M = (date.getMonth() + 1);
        }
        if(date.getDate() < 10) {
          D = '0' + date.getDate();
        }else {
          D = date.getDate();
        }
        return date.getFullYear() + '-' + M + '-' + D;
      }else {
        return '';
      }
    },
  },
  methods: {
    // 毫秒处理
    crtTimeFtt(val, row) {
      if (val) {
        var date = new Date(val);
        let M = 0, D = 0
        if((date.getMonth() + 1) < 10) {
          M = '0'+ (date.getMonth() + 1)
        }else {
          M = (date.getMonth() + 1);
        }
        if(date.getDate() < 10) {
          D = '0' + date.getDate();
        }else {
          D = date.getDate();
        }
        return date.getFullYear() + '-' + M + '-' + D;
      }else {
        return '';
      }
    },
    // 函数节流工具函数
    throttle(fn, delay) {
      let timer = null;

      return function () {
        clearTimeout(timer);
        timer = setTimeout(function() {
          fn();
        }, delay);
      }

    },
    resetTime(){
        let obj = {
          min: '',
          max: ''
        }
        const date = new Date();
        const startTime = date.getFullYear() + '-1-1';
        const nowTime = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
        obj.min = startTime;
        obj.max = nowTime;
        return obj
    }
  }
};
/*外链接跳转*/
export const href = {
  methods: {
    see(href) {
      //console.log(href);
      if (href.indexOf("http") == -1) {
        window.location.href = "https://" + href;
      }
      window.location.href = href;
    }
  }
};
// 字符串处理过滤器
export const stringTrim = {
  methods: {
    // 删除空格
    trim(str) {
      if (str) {
        return str.replace(/(^\s*)|(\s*$)/g, "");
      } else {
        return str;
      }

    }
  },
  filters: {
    // 删除负号
    delMinus(val) {
      if(!val) {
        return 0
      }
      return String(val).substr(1);

    }
  }
};
// 三级联动
export const selectAddress = {
  data() {
    return {
      // 详细的描述的地址
      address: {
          province: "",
          city: "",
          prefecture: "",
          detail: ""
      },
    }
  },
  methods: {
    changeProvince(data) {
      this.address.province = data.value;
    },
    changeCity(data) {
      this.address.city = data.value;
    },
    changeArea(data) {
      this.address.prefecture = data.value;
    },
    // 处理联系地址
    dealAddress() {
      const that = this;
      return (
        that.address.province +
        that.address.city +
        that.address.prefecture +
        that.address.detail
      );
    },
    // 选择时间
    changeTime(val,flag) {
      const that  = this;
      switch (flag) {
        case 1:
          that.time.min = val;
          break;
        case 2:
          that.time.max = val;
          break;
      }
    },
  }
};

